<!doctype html><html lang=cn-zh><head><meta charset=UTF-8><meta name=viewport content="width=device-width,initial-scale=1"><meta http-equiv=X-UA-Compatible content="IE=edge"><style type=text/css>body{font-family:monospace}</style><title>Python-高阶函数习题练习</title>
<meta name=description content="A blog maintained by Vimiix."><link rel=stylesheet href=/css/style.css><script>var _hmt=_hmt||[];(function(){var e,t=document.createElement("script");t.src="https://hm.baidu.com/hm.js?7c24231917964240bae97e813810620c",e=document.getElementsByTagName("script")[0],e.parentNode.insertBefore(t,e)})()</script></head><body><header>====================<br>== Hi, I'm Vimiix ==<br>====================<div style=float:right;color:gray;font-size:x-large>Get hands dirty.</div><br><p><nav><a href=https://www.vimiix.com/><b>首页</b></a>.
<a href=/posts/><b>文章列表</b></a>.
<a href=/projects/><b>开源项目</b></a>.
<a href=/tags/><b>标签</b></a>.
<a href=/friends/><b>友链</b></a>.
<a href=/about/><b>关于我</b></a>.
<a href=/index.xml><b>RSS</b></a>.</nav></p></header><main><article><h1>Python-高阶函数习题练习</h1><b><time>2017.04.08 15:50</time></b>
<a href=/tags/map>map</a>
<a href=/tags/reduce>reduce</a>
<a href=/tags/filter>filter</a>
<a href=/tags/python>Python</a><div><p><figure><img src=http://vimiix-blog.oss-cn-qingdao.aliyuncs.com/map-reduce-filter.jpg alt></figure></p><h5 id=本文是针对mapreduce和filter三个高阶函数的程序练习>本文是针对map()，reduce()和filter()三个高阶函数的程序练习。</h5><p>习题来自：<a href=http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001386819873910807d8c322ca74d269c9f80f747330a52000>廖雪峰的python2.7教程 - map/reduce</a></p><h3 id=map概念>map()概念</h3><p>map()函数接收两个参数，一个是函数，一个是序列，map将传入的函数依次作用到序列的每个元素，并把结果作为新的列表返回。</p><h5 id=题目>题目</h5><blockquote><p>利用map()函数，把用户输入的不规范的英文名字，变为首字母大写，其他小写的规范名字。
例如输入：[&lsquo;adam&rsquo;, &lsquo;LISA&rsquo;, &lsquo;barT&rsquo;]，输出：[&lsquo;Adam&rsquo;, &lsquo;Lisa&rsquo;, &lsquo;Bart&rsquo;]</p></blockquote><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-Python data-lang=Python><span style=display:flex><span>	<span style=color:#f92672>&gt;&gt;&gt;</span> <span style=color:#66d9ef>def</span> <span style=color:#a6e22e>test</span>(name_list):
</span></span><span style=display:flex><span>		    print(map(<span style=color:#66d9ef>lambda</span> name: name[<span style=color:#ae81ff>0</span>]<span style=color:#f92672>.</span>upper()<span style=color:#f92672>+</span>name[<span style=color:#ae81ff>1</span>:]<span style=color:#f92672>.</span>lower(), name_list))
</span></span><span style=display:flex><span>		
</span></span><span style=display:flex><span>	<span style=color:#f92672>&gt;&gt;&gt;</span> test([<span style=color:#e6db74>&#39;adam&#39;</span>,<span style=color:#e6db74>&#39;LISA&#39;</span>,<span style=color:#e6db74>&#39;barT&#39;</span>])
</span></span><span style=display:flex><span>	[<span style=color:#e6db74>&#39;Adam&#39;</span>, <span style=color:#e6db74>&#39;Lisa&#39;</span>, <span style=color:#e6db74>&#39;Bart&#39;</span>]
</span></span><span style=display:flex><span>	<span style=color:#f92672>&gt;&gt;&gt;</span> 
</span></span></code></pre></div><h3 id=reduce概念>reduce()概念</h3><p>reduce()把一个函数作用在一个序列[x1, x2, x3&mldr;]上，这个函数必须接收两个参数，reduce把结果继续和序列的下一个元素做累积计算</p><h5 id=题目-1>题目</h5><blockquote><p>Python提供的sum()函数可以接受一个<strong>list</strong>并求和，请编写一个prod()函数，可以接受一个list并利用reduce()求积</p></blockquote><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-Python data-lang=Python><span style=display:flex><span>	<span style=color:#f92672>&gt;&gt;&gt;</span> <span style=color:#66d9ef>def</span> <span style=color:#a6e22e>prod</span>(num_list):
</span></span><span style=display:flex><span>		    print reduce(<span style=color:#66d9ef>lambda</span> a,b : a<span style=color:#f92672>*</span>b , num_list)
</span></span><span style=display:flex><span>
</span></span><span style=display:flex><span>	<span style=color:#f92672>&gt;&gt;&gt;</span> prod([<span style=color:#ae81ff>1</span>,<span style=color:#ae81ff>2</span>,<span style=color:#ae81ff>3</span>,<span style=color:#ae81ff>4</span>])
</span></span><span style=display:flex><span>	<span style=color:#ae81ff>24</span>
</span></span></code></pre></div><h3 id=filter概念>filter概念</h3><p>filter()接收一个函数和一个序列，filter()把传入的函数依次作用于每个元素，然后根据返回值是True还是False决定保留还是丢弃该元素</p><h5 id=题目-2>题目</h5><blockquote><p>请尝试用filter()删除1~100的素数</p></blockquote><p>概念补充： 素数，又称质数（prime number），有无限个。素数定义为在大于1的自然数中，除了1和它本身以外不再有其他因数的数称为质数</p><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-Python data-lang=Python><span style=display:flex><span>	<span style=color:#75715e>#coding=utf-8</span>
</span></span><span style=display:flex><span>	
</span></span><span style=display:flex><span>	<span style=color:#75715e>#判断是不是素数，不是返回True，是返回False</span>
</span></span><span style=display:flex><span>	<span style=color:#66d9ef>def</span> <span style=color:#a6e22e>not_prime</span>(num):
</span></span><span style=display:flex><span>	      <span style=color:#66d9ef>if</span>(num <span style=color:#f92672>&lt;</span> <span style=color:#ae81ff>2</span>):
</span></span><span style=display:flex><span>	            <span style=color:#66d9ef>return</span> <span style=color:#66d9ef>True</span>
</span></span><span style=display:flex><span>	      judge <span style=color:#f92672>=</span> <span style=color:#ae81ff>2</span>
</span></span><span style=display:flex><span>	      <span style=color:#66d9ef>while</span>(judge <span style=color:#f92672>&lt;</span> num):
</span></span><span style=display:flex><span>	            <span style=color:#66d9ef>if</span> num<span style=color:#f92672>%</span>judge <span style=color:#f92672>==</span> <span style=color:#ae81ff>0</span>:
</span></span><span style=display:flex><span>	                  <span style=color:#66d9ef>return</span> <span style=color:#66d9ef>True</span>
</span></span><span style=display:flex><span>	            judge <span style=color:#f92672>+=</span> <span style=color:#ae81ff>1</span>
</span></span><span style=display:flex><span>	      <span style=color:#66d9ef>return</span> <span style=color:#66d9ef>False</span>
</span></span><span style=display:flex><span>	
</span></span><span style=display:flex><span>	<span style=color:#75715e>#将一个数字列表中所有的素数过滤删除掉</span>
</span></span><span style=display:flex><span>	<span style=color:#66d9ef>def</span> <span style=color:#a6e22e>prime_number</span>(num_list):
</span></span><span style=display:flex><span>	      print filter(not_prime,num_list)
</span></span><span style=display:flex><span>	
</span></span><span style=display:flex><span>	<span style=color:#75715e>#删除1~100以内的素数</span>
</span></span><span style=display:flex><span>	prime_number(range(<span style=color:#ae81ff>100</span>))
</span></span></code></pre></div><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-Python data-lang=Python><span style=display:flex><span>	运行结果<span style=color:#960050;background-color:#1e0010>：</span>
</span></span><span style=display:flex><span>	<span style=color:#f92672>====================</span> RESTART: E:\Python\practices\test<span style=color:#f92672>.</span>py <span style=color:#f92672>====================</span>
</span></span><span style=display:flex><span>	[<span style=color:#ae81ff>0</span>, <span style=color:#ae81ff>1</span>, <span style=color:#ae81ff>4</span>, <span style=color:#ae81ff>6</span>, <span style=color:#ae81ff>8</span>, <span style=color:#ae81ff>9</span>, <span style=color:#ae81ff>10</span>, <span style=color:#ae81ff>12</span>, <span style=color:#ae81ff>14</span>, <span style=color:#ae81ff>15</span>, <span style=color:#ae81ff>16</span>, <span style=color:#ae81ff>18</span>, <span style=color:#ae81ff>20</span>, <span style=color:#ae81ff>21</span>, <span style=color:#ae81ff>22</span>, <span style=color:#ae81ff>24</span>, <span style=color:#ae81ff>25</span>, <span style=color:#ae81ff>26</span>, <span style=color:#ae81ff>27</span>, <span style=color:#ae81ff>28</span>, <span style=color:#ae81ff>30</span>, <span style=color:#ae81ff>32</span>, <span style=color:#ae81ff>33</span>, <span style=color:#ae81ff>34</span>, <span style=color:#ae81ff>35</span>, <span style=color:#ae81ff>36</span>, <span style=color:#ae81ff>38</span>, <span style=color:#ae81ff>39</span>, <span style=color:#ae81ff>40</span>, <span style=color:#ae81ff>42</span>, <span style=color:#ae81ff>44</span>, <span style=color:#ae81ff>45</span>, <span style=color:#ae81ff>46</span>, <span style=color:#ae81ff>48</span>, <span style=color:#ae81ff>49</span>, <span style=color:#ae81ff>50</span>, <span style=color:#ae81ff>51</span>, <span style=color:#ae81ff>52</span>, <span style=color:#ae81ff>54</span>, <span style=color:#ae81ff>55</span>, <span style=color:#ae81ff>56</span>, <span style=color:#ae81ff>57</span>, <span style=color:#ae81ff>58</span>, <span style=color:#ae81ff>60</span>, <span style=color:#ae81ff>62</span>, <span style=color:#ae81ff>63</span>, <span style=color:#ae81ff>64</span>, <span style=color:#ae81ff>65</span>, <span style=color:#ae81ff>66</span>, <span style=color:#ae81ff>68</span>, <span style=color:#ae81ff>69</span>, <span style=color:#ae81ff>70</span>, <span style=color:#ae81ff>72</span>, <span style=color:#ae81ff>74</span>, <span style=color:#ae81ff>75</span>, <span style=color:#ae81ff>76</span>, <span style=color:#ae81ff>77</span>, <span style=color:#ae81ff>78</span>, <span style=color:#ae81ff>80</span>, <span style=color:#ae81ff>81</span>, <span style=color:#ae81ff>82</span>, <span style=color:#ae81ff>84</span>, <span style=color:#ae81ff>85</span>, <span style=color:#ae81ff>86</span>, <span style=color:#ae81ff>87</span>, <span style=color:#ae81ff>88</span>, <span style=color:#ae81ff>90</span>, <span style=color:#ae81ff>91</span>, <span style=color:#ae81ff>92</span>, <span style=color:#ae81ff>93</span>, <span style=color:#ae81ff>94</span>, <span style=color:#ae81ff>95</span>, <span style=color:#ae81ff>96</span>, <span style=color:#ae81ff>98</span>, <span style=color:#ae81ff>99</span>]
</span></span></code></pre></div></div></article></main><aside><div><div><h3>LATEST POSTS</h3></div><div><ul><li><a href=/posts/2025-10-16-kubernetes-apiserver-authorization-mechanism/>Kubernetes APIServer 鉴权机制</a></li><li><a href=/posts/2025-09-30-kubernetes-apiserver-authentication-mechanism/>Kubernetes APIServer 认证机制</a></li><li><a href=/posts/2024-12-16-deploy-kubernetes-by-kubeadm/>使用 kubeadm 搭建 kubernetes 集群</a></li><li><a href=/posts/2024-09-20-how-to-code-review/>如何做code review</a></li><li><a href=/posts/2024-08-12-weakref-in-python/>Python中的弱引用</a></li></ul></div></div></aside><footer><p>Social Links:
<a href=https://github.com/vimiix><b>Github</b></a>.
<a href=https://www.douban.com/people/vimiix/><b>Douban</b></a>.
<a href=mailto:i@vimiix.com><b>Email</b></a>.<br><hr>&copy; 2017-2025
Vimiix Yao; All rights reserved.
<a href=https://beian.miit.gov.cn/>京ICP备19015214号-1</a></p><script src=https://l2dwidget.js.org/lib/L2Dwidget.min.js></script><script>L2Dwidget.init({model:{jsonPath:"https://unpkg.com/live2d-widget-model-tororo@1.0.5/assets/tororo.model.json"}})</script></footer></body></html>